package com.etung.iot.demo.handler;

import com.alibaba.fastjson.JSONObject;
import com.etung.iot.demo.configs.TopicConfig;
import com.etung.iot.demo.constant.CommonConstant;
import com.etung.iot.demo.constant.WebSocketConstant;
import com.etung.iot.demo.dao.PlcStatusMapper;
import com.etung.iot.demo.dao.UserMapper;
import com.etung.iot.demo.dao.VariantMapper;
import com.etung.iot.demo.modles.PlcStatus;
import com.etung.iot.demo.utils.RedisUtil;
import com.etung.iot.demo.utils.SpringContextUtils;
import com.etung.iot.demo.websocket.MyWebSocket;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@Handler(TopicConfig.PLC_STATUS_TOPIC)
@Slf4j
public class PlcStatusHandler implements TopicHandler {
    @Autowired
    private PlcStatusMapper plcStatusMapper;
    @Autowired
    private MyWebSocket myWebSocket;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private VariantMapper variantMapper;
    @Autowired
    private UserCacheUtil userCacheUtil;


    @Override
    public void handler(String topic,String payload) {
        log.info("payload:"+payload);
        JSONObject jsonObject = JSONObject.parseObject(payload);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<PlcStatus> plcStatusList = Lists.newArrayList();
        String imei = "";
        Date time = null;
        if(jsonObject.containsKey("imei") && jsonObject.containsKey("time")){
            imei = jsonObject.getString("imei");
            time = new Date(jsonObject.getLong("time"));
        }
        for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
            if(StringUtils.isNumeric(entry.getKey())){
                PlcStatus plcStatus = new PlcStatus();
                plcStatus.setImei(imei);
                plcStatus.setTime(time);
                plcStatus.setDeviceId(Integer.parseInt(entry.getKey()));
                plcStatus.setValue(String.valueOf(entry.getValue()));
                plcStatusList.add(plcStatus);
                //离线处理缓存
                if ("0".equals(plcStatus.getValue())){
                    List<String> varIdList = variantMapper.selectVarIdByDeviceId(imei,plcStatus.getDeviceId());
                    if(varIdList != null && varIdList.size() > 0){
                        String finalImei = imei;
                        varIdList.forEach(varId->{
                            String cacheKey = CommonConstant.REAL_DATA_CACHE_PREFIX + finalImei +'_' +  varId;
                            redisUtil.del(cacheKey);
                        });
                    }
                }
            }
        }
        if (plcStatusList.size() > 0){
            jsonObject.put("msg_type",TopicConfig.MESSAGE_TYPE_PLC_STATUS);
            String uid = userCacheUtil.getUid(imei);
            if(!StringUtils.isEmpty(uid)){
                myWebSocket.pushRealData(uid,jsonObject.toJSONString());
            }
            plcStatusMapper.insertList(plcStatusList);
        }
    }
}
